package com.ruoyi.web.controller.modules.yami.admin;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.yami.shop.domain.User;
import com.yami.shop.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/user")
public class UserController extends BaseController {
    @Autowired
    private IUserService userService;

    /**
     * 分页获取
     */
    @GetMapping("/page")
    public TableDataInfo page(User user)
    {
        startPage();
        List<User> list = userService.list(new LambdaQueryWrapper<User>()
                .like(StrUtil.isNotBlank(user.getNickName()), User::getNickName, user.getNickName())
                .eq(user.getStatus() != null, User::getStatus, user.getStatus()));
        for (User user1 : list) {
            user1.setNickName(user1.getNickName() == null ? "" : user1.getNickName());
        }
        return getDataTable(list);
    }

    //@Log(title = "用户管理", businessType = BusinessType.INSERT)
    //@PostMapping
    //public AjaxResult add(@Validated @RequestBody User user)
    //{
    //
    //}
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult update(@Validated @RequestBody User user)
    {
        user.setNickName(user.getNickName() == null ? "" : user.getNickName());
        user.setUpdateBy(getUsername());
        return toAjax(userService.updateUser(user));
    }

    @GetMapping(value = "/{userId}")
    public AjaxResult getInfo(@PathVariable String userId) {
        return success(userService.getUserByUserId(userId));
    }
}
